package lichbaogiang;

import java.awt.*;
import java.awt.event.*;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;


public class GiangVien extends JFrame{

    //-------------
    String st1 = "";
    String st2 = "";
    String st3 = "";
    String st4 = "";
    String st5 = "";
    String st6 = "";
    String st7 = "";
    String st8 = "";
    String st9 = "";
    String st10 = "";
    static int checkInsert = 0;
    //public static String dsgv[][]= new String[100][4];
    public static int count_row = 0;
    JTable table = new JTable();
    DefaultTableModel tablemodel = new DefaultTableModel();
    String data1[] = new String[20];
    JPanel pn = new JPanel(new BorderLayout());
    JFrame f = new JFrame();
    JToolBar toolBar;
    InsertTeacher insert;
    JLabel lbMH;
    String url = "jdbc:odbc:CNPM";
    //--------DataBase Connection--------//
    Connection conn;
    Statement stmt;
    ResultSet rs;
    ResultSetMetaData rsmd;
    
    
    int count_col=0;
    
    public void gui()
    {
        
        f.setSize(800, 600);
        f.setVisible(true);
        toolBar = new JToolBar();
        toolBar.setEnabled(false);
        toolBar.setBackground(Color.decode("#FFE4B5"));
        toolBar.addSeparator(new Dimension(70,0));
        table.addKeyListener(null);
        //add but into toolbar
        //--------Insert--------//
        ImageIcon icon = new ImageIcon(getClass().getResource("icon/Insert.gif"));
	JButton but = new JButton(icon); 
        but.setText("Chèn");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Chèn");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                InsertInformationSubject(e);
            }
        }); 
	toolBar.add(but); 
        toolBar.addSeparator(new Dimension(100,50));
        
        //---------Delete----------//
        icon = new ImageIcon(getClass().getResource("icon/Delete.gif"));
	but = new JButton(icon); 
        but.setText("Xóa");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Xóa");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                DeleteInformationSubject(e);
            }
        }); 
	toolBar.add(but); 
        toolBar.addSeparator(new Dimension(100,50));
        
        //-------Update-------//
        icon = new ImageIcon(getClass().getResource("icon/Update.gif"));
	but = new JButton(icon); 
        but.setText("Cập Nhật");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Cập Nhật");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                UpdateInformationSubject(e);
            }
        }); 
	toolBar.add(but);
        toolBar.addSeparator(new Dimension(100,50));
        
        //--------Exit--------//
        icon = new ImageIcon(getClass().getResource("icon/Exit.gif"));
	but = new JButton(icon); 
        but.setText("Thoát");
        but.setBackground(Color.decode("#00FFFF"));
	but.setToolTipText("Thoát");  
	but.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                if(JOptionPane.showConfirmDialog(null,"Bạn muốn kết thúc?","",JOptionPane.YES_NO_OPTION) == 0) 
                {
                    f.setVisible(false);
                }
            }
        }); 
	toolBar.add(but);
        
        pn.setBackground(Color.green);
        lbMH = new JLabel("DANH SÁCH GIẢNG VIÊN");
        pn.add(lbMH, BorderLayout.NORTH);
        table.addKeyListener(new KeyAdapter() {
            public void keypressed(KeyEvent evt)
            {
                TableKeyPressed(evt);
            }
});
        pn.add(new JScrollPane(table), BorderLayout.CENTER);
        f.add(pn, BorderLayout.CENTER);
        f.add(toolBar, BorderLayout.NORTH);
        f.setTitle("GIẢNG VIÊN");
    }
    
    public void loadingdata(String sql)
    {
            String colum[] = {"MSGV","TenGV","SDT","Email","NamBatDau","HSThamnien","HSChuyenMon","HSNCKH","Diem","BoMon"}; 
            //columNames = colum;
            tablemodel.setColumnIdentifiers(colum);
            table.setModel(tablemodel);
            
            
            //String data1[] = new String[20];
            try{
            //Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection(url);
            stmt = conn.createStatement();
            rs = stmt.executeQuery(sql);
            rsmd = rs.getMetaData();
                System.out.println("connection start....");
            while(rs.next())
            { 
                data1[0] = rs.getString("MSGV");
                //dsgv[count_row][0]=data1[0];
                data1[1] = rs.getString("TenGV");
                //dsgv[count_row][1]=data1[1];
                data1[2] = rs.getString("SDT");
                data1[3] = rs.getString("Email");
                data1[4] = String.valueOf(rs.getInt("NamBatDau"));   
                data1[5] = String.valueOf(rs.getFloat("HSThamNien"));
                data1[6] = String.valueOf(rs.getInt("HSChuyenMon"));
                data1[7] = String.valueOf(rs.getInt("HSNCKH"));
                data1[8] = String.valueOf(rs.getFloat("Diem"));
                //dsgv[count_row][2]=data1[8];
                data1[9] = rs.getString("BoMon");
                //dsgv[count_row][3]=data1[9];
                tablemodel.addRow(data1);
                count_row++;                
            }
        } catch(Exception ex){  
          System.out.println("Error " + ex); }
        }
    public GiangVien()
    {
        gui();
        String sql = "select * from GiangVien1";
        loadingdata(sql);
    }
/*
    public static void main(String[] args)
    {
        new GiangVien();
    }
*/
    public void InsertInformationSubject(ActionEvent e)
    {
        checkInsert = 1 - checkInsert;
        if(checkInsert != 0){
            lbMH.setText("");
            insert = new InsertTeacher();
            pn.add(insert, BorderLayout.NORTH);
            f.setVisible(true);
        }
        else{
            lbMH.setText("DANH SÁCH GIẢNG VIÊN");
            insert.hide();
            pn.add(lbMH, BorderLayout.NORTH);
        }
    }
    
    public void DeleteInformationSubject(ActionEvent e)
    {
        //insert.hide();
        int  row = table.getSelectedRow();
        String st = (String) table.getValueAt(table.getSelectedRow(), 0);
        String sql = "delete from GiangVien1 where MSGV = '"+st+"'";
        if(JOptionPane.showConfirmDialog(null,"Bạn muốn xóa không?","Yes/No!",JOptionPane.YES_NO_OPTION) == 0) 
        {
            tablemodel.removeRow(row);
            loadingdata(sql);
            count_row --;
        }
    }
    
    public void UpdateInformationSubject(ActionEvent e)
    {
        if(JOptionPane.showConfirmDialog(null,"Bạn muốn lưu lại không?","Thông báo",JOptionPane.YES_NO_OPTION)==0) 
        {
            checkInsert = 1 - checkInsert;
            
            String sql="";
            int kt=tablemodel.getRowCount();
            for(int j=0;j<kt;j++)
            {
                st1=(String)tablemodel.getValueAt(j, 0);
                st2=(String)tablemodel.getValueAt(j, 1);
                st3=(String)tablemodel.getValueAt(j, 2);
                st4=(String)tablemodel.getValueAt(j, 3);
                st5=(String)tablemodel.getValueAt(j, 4);
                st6=(String)tablemodel.getValueAt(j, 5);
                st7=(String)tablemodel.getValueAt(j, 6);
                st8=(String)tablemodel.getValueAt(j, 7);
                st9=(String)tablemodel.getValueAt(j, 8);
                st10=(String)tablemodel.getValueAt(j, 9);
                sql="update Giangvien1 set TenGV='"+st2+"', SDT='"
                        +st3+"', Email='"+st4+"', NamBatDau='"
                        +st5+"', HSThamNien='"+st6+"', HSChuyenMon='"
                        +st7+"' ,HSNCKH='"+st8+"', Diem='"+st9+"', BoMon='"
                        +st10+"' where MSGV='"+st1+"'";
                        try {
                        Connection con = DriverManager.getConnection(url);
                        Statement stm = con.createStatement();
                        stm.executeUpdate(sql);
                    } catch (SQLException ex) {
                    //Logger.getLogger(Lichbaogiang.class.getName()).log(Level.SEVERE, null, ex);
            }
            
        }
    }
        f.setVisible(false);
        new GiangVien();
    }
    private void TableKeyPressed(java.awt.event.KeyEvent evt) {                                  
        int dong=table.getSelectedRow();
        int cot=table.getSelectedColumn();
        TableCellEditor tam= table.getCellEditor(cot,dong);
        table.setValueAt(tam, cot, dong);
    }
}